/**
 * 
 */
package leetCode;

/**
 * @author zhong
 *
 */
public class AvailableCapturesforRook {
	public int numRookCaptures(char[][] board) {
		int[] whitePos = getPos(board);
		int[][] dirs = { { 0, 1 }, { 0, -1 }, { -1, 0 }, { 1, 0 } };
		int res = 0;
		for (int[] dir : dirs) {
			if (findEat(whitePos, dir, board)) {
				res++;
			}
			;
		}
		return res;
	}

	private boolean findEat(int[] whitePos, int[] dir, char[][] board) {
		int[] curPos = whitePos;
		while (true) {
			curPos[0] += dir[0];
			curPos[1] += dir[1];
			if (outOfBound(board.length, curPos)) {
				return false;
			}
			if (board[curPos[0]][curPos[1]] == 'p') {
				return true;
			}
			if (board[curPos[0]][curPos[1]] == 'B') {
				return false;
			}
		}
	}

	private boolean outOfBound(int length, int[] curPos) {
		if (curPos[0] < 0 || curPos[1] < 0 || curPos[0] >= length || curPos[1] >= length) {
			return true;
		}
		return false;
	}

	private int[] getPos(char[][] board) {
		for (int i = 0; i < board.length; i++) {
			for (int j = 0; j < board[0].length; j++) {
				if (board[i][j] == 'R') {
					return new int[] { i, j };
				}
			}
		}
		return new int[] { -1, -1 };
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
